//
//  CZStatusOriginalView.swift
//  微博08
//
//  Created by apple on 16/3/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
//微博原创内容
let CZStatusCellMargin: CGFloat = 8.0

// 封装用户信息和微博文本
class CZStatusOriginalView: UIView {
    
    // MARK: - 属性
    
    // 底部约束的属性
    var statusOriginalViewBottom: Constraint?
    /// 微博模型
    var status: CZStatus? {
        didSet {
            // 当cell给CZStatusOriginalView设置status模型时,给里面的控件设置内容
            
            // 设置头像
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            // 设置用户名
            nameLabel.text = status?.user?.screen_name
            
            // 设置时间
            // 7-12-3
            //在cell将要显示的时候来计算时间这一能够保证cell显示的时候能够刷新时间
           timeLabel.text = NSDate.sinaDateStringToDate(status?.created_at ?? "").sinaDateDescription()
          
            //timeLabel.text = status?.created_at
            
            // 设置来源
            sourceLabel.text = status?.source
            
            // 设置微博内容
            contentLabel.text = status?.text
            
            // 设置会员等级
            //            if let mbrank = status?.user?.mbrank {
            //                if mbrank > 0 && mbrank <= 6 {
            //                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
            //                } else {
            //                    memberView.image = nil
            //                }
            //            }
            memberView.image = status?.user?.mbrankImage
            
            // 设置认证图标
            verifiedTypeImage.image = status?.user?.verifiedTypeImage
            //            if let verified_type = status?.user?.verified_type {
            //                var image: UIImage? = nil
            //                switch verified_type {
            //                case 0:
            //                    image = UIImage(named: "avatar_vip")
            //                case 2,3,5:
            //                    image = UIImage(named: "avatar_enterprise_vip")
            //                case 220:
            //                    image = UIImage(named: "avatar_grassroot")
            //                default:
            //                    image = nil
            //                }
            //
            //                verifiedTypeImage.image = image
            //            }
            
            // 判断是否需要显示配图
            // 去掉之前的底部约束
            statusOriginalViewBottom?.uninstall()
            if let picture = status?.pic_urls where picture.count > 0 {
                pictureView.hidden = false
                // 有配图需要显示 设置配图的status
                pictureView.status = status
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusOriginalViewBottom = make.bottom.equalTo(pictureView).offset(CZStatusCellMargin).constraint
                })
            } else {
                // 不需要显示配图
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusOriginalViewBottom = make.bottom.equalTo(contentLabel).offset(CZStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 2.添加约束
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 认证图标
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(CZStatusCellMargin)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        // 微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        // 设置originalView的底部和pictureView的底部重合
        self.snp_makeConstraints { (make) -> Void in
            // 底部约束不一定都是约束在pictureView的底部
            self.statusOriginalViewBottom = make.bottom.equalTo(pictureView).offset(CZStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    // 用户名称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
    
    // 会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), font: 11)
    
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: 11)
    
    // 认证类型
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    
    // 微博内容的label
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        
        // 设置换行
        label.numberOfLines = 0
        
        return label
    }()
    
    // 微博配图
   
    private lazy var pictureView:CycStatusPrictureView=CycStatusPrictureView()
}
//显示原创微博配图内容08